package com.yookey.controller;


import com.yookey.common.constant.ResponseMessageEnum;
import com.yookey.common.exception.ParamException;
import com.yookey.common.result.Result;
import com.yookey.common.utils.AssertUtil;
import com.yookey.model.dto.AccountDTO;
import com.yookey.model.dto.PasswordDTO;
import com.yookey.model.entity.Account;
import com.yookey.service.AccountService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yww
 * @date 2020/9/29
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @ApiOperation(value = "用户新增")
    @PostMapping(value = "/add")
    public Result add(@RequestBody AccountDTO accountDTO) {
        return accountService.add(accountDTO);
    }

    @ApiOperation(value = "用户删除")
    @DeleteMapping(value = "/del/{id}")
    public Result add(@PathVariable Integer id) {
        return accountService.del(id);
    }

    @ApiOperation(value = "用户信息修改")
    @PutMapping(value = "/edit")
    public Result edit(@RequestBody AccountDTO accountDTO) {

        return accountService.edit(accountDTO);
    }

    @ApiOperation(value = "分页查询用户列表")
    @GetMapping(value = "/page")
    public Result page(@RequestParam int page, @RequestParam int limit) {
        if (StringUtils.isBlank(String.valueOf(page)) || StringUtils.isEmpty(String.valueOf(limit))) {
            page = 1;
            limit = 10;
        }
        return accountService.getPage(page, limit);
    }

    @ApiOperation(value = "查询所有用户列表")
    @GetMapping(value = "/list")
    public Result listAccount() {
        return Result.success(accountService.list());
    }

    @ApiOperation(value = "用户状态修改")
    @PostMapping(value = "/changeState")
    public Result changeState(@RequestBody AccountDTO accountDTO) {

        return accountService.changeState(accountDTO);
    }

    @ApiOperation(value = "查看用户信息")
    @GetMapping(value = "/getAccountInfo/{id}")
    public Result getAccountInfo(@PathVariable Integer id) {
        Account byId = accountService.getById(id);
        return Result.success(byId);
    }

    @ApiOperation(value = "头像修改")
    @PutMapping(value = "/avatar")
    public Result updateAvatar(String id, String path) {
        if (path == null || id == null) {
            throw new ParamException(ResponseMessageEnum.PARAMETRIC_ERROR);
        }
        Account byId = accountService.getById(id);
        AssertUtil.isNull(byId, "该用户不存在");
        Integer integer = accountService.updateAvatar(id, path);
        if (integer > 0) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }


    @ApiOperation(value = "密码修改")
    @PutMapping("/pwd")
    public Result updatePassword(@RequestBody PasswordDTO passwordDTO) {
        Integer integer = accountService.modifyPassword(passwordDTO);
        if (integer > 0) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }
}
